package reflect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;

/**
 * @author wangyingjie
 * @date
 * @remark 自定义invocationHandle类
 */
public class MyInvocationHandler implements InvocationHandler {
    /**
     *
     * @param proxy 代理对象
     * @param method 运行的方法
     * @param args 传参
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("生成的代理类的实现的接口是:↓");
        Arrays.stream(proxy.getClass().getInterfaces()).forEach(System.out::println);
        System.out.println("正在的执行的方法是："+method.getName());
        if (args== null || args.length== 0){
            System.out.println("空参");
        }else {
            Arrays.stream(args).forEach(System.out::println);
        }
        return null;
    }

    public static void main(String[] args) {
        MyInvocationHandler myInvocationHandle = new MyInvocationHandler();
        Person p = (Person)Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(), new Class[]{Person.class}, myInvocationHandle);
        p.walk();
        p.sayHello("小王");
    }
}
